package com.zlq.easydbutils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

/**
 * 
 * @author liujia
 *
 */
public class Runner {

	/**
	 * Runner是QueryRunner的代理对象
	 */
	private static final QueryRunner runner = new QueryRunner();
	private ThreadLocal<Connection> local = null;

	public Runner(ThreadLocal<Connection> local) {
		this.local = local;
	}

	public int[] batch(String sql, Object[][] params) throws SQLException {
		return runner.batch(local.get(), sql, params);
	}

	public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params)
			throws SQLException {
		return runner.query(local.get(), sql, rsh, params);
	}

	public <T> List<T> querys(String sql, BeanListHandler<T> rsh,
			Object... params) throws SQLException {
		return runner.query(local.get(), sql, rsh, params);
	}
	
	public List<Map<String, Object>> querysMap(String sql, MapListHandler rsh,
			Object... params) throws SQLException {
		return runner.query(local.get(), sql, rsh, params);
	}
	
	public  List querysArray(String sql, ArrayListHandler rsh,
			Object... params) throws SQLException {
		return runner.query(local.get(), sql, rsh, params);
	}

	public <T> T query(String sql, ResultSetHandler<T> rsh) throws SQLException {
		return runner.query(local.get(), sql, rsh);
	}

	public int update(String sql, Object... params) throws SQLException {
		return runner.update(local.get(), sql, params);
	}

	public int update(String sql, Object param) throws SQLException {
		return runner.update(local.get(), sql, param);
	}

	public int update(String sql) throws SQLException {
		return runner.update(local.get(), sql);
	}

}
